package org.xtext.runtime.interpreter;

import java.util.HashMap;
import java.util.Map;

import org.xtext.runtime.formula.Event;
import org.xtext.runtime.formula.IFormula;
import org.xtext.runtime.plan.IPlanStep;
import org.xtext.runtime.term.Variable;
import org.xtext.runtime.util.Bindings;

public class Rule {
	private Event event;
	private IFormula context;
	private IPlanStep plan;
	
	public Rule(Event event, IFormula context, IPlanStep plan) {
		this.event = event;
		this.context = context;
		this.plan = plan;
	}
	
	public Event event() {
		return event;
	}
	
	public IFormula context() {
		return context;
	}
	
	public IPlanStep plan() {
		return plan;
	}
	
	public String toString() {
		return "rule " + event + " : " + context + " " + plan;
	}
	
	public String toFullString(String prefix) {
		return prefix + "rule " + event + " : " + context + " " + plan.toFullString("\t");
	}
	
	public Rule copy() {
		Map<String, Variable> vars = new HashMap<String, Variable>();
		return new Rule( (Event) event.copy(vars), context.copy(vars), plan.copy(vars) );
	}

	public Rule apply(Bindings bindings) {
//		System.out.println("BEFORE: " + event() + " / " + plan().variables());
		event = (Event) event.apply(bindings);
		context = context.apply(bindings);
		plan = plan.apply(bindings);

//		Rule rule = copy();
//		System.out.println("AFTER: " + rule.event() + " / " + rule.plan().variables());
//		rule.event.apply(bindings);
//		rule.context.apply(bindings);
//		rule.plan.apply(bindings);
//		System.out.println("AFTER x2: " + rule.event() + " / " + rule.plan().variables());
//		return rule;
		return this;
	}
}
